package com.wzdigit.wms.raw.controller.transfer;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.basic.client.Pager;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.transfer.CommitTransferReq;
import com.wzdigit.wms.basic.client.request.transfer.SearchTransferManageRequest;
import com.wzdigit.wms.basic.client.request.transfer.SearchTransferTaskRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.transfer.*;
import com.wzdigit.wms.raw.client.response.asn.report.ortherasn.OrtherAsnEnums;
import com.wzdigit.wms.raw.constant.RawWareConstant;
import com.wzdigit.wms.raw.service.transfer.TransferAutoService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 原材料调拨PDA
 * Created by zyf on 2021/8/11 23:31
 */
@Slf4j
@RestController
@RequestMapping(value = RawWareConstant.API_VERSION)
@Api(tags = "原材料调拨PDA")
public class TransferAutoController {
    public static final String REQUEST_MODEL = "/transferauto";

    @Autowired
    private TransferAutoService transferAutoService;

    @ApiOperation(value = "【前端加个确认按钮】关闭调拨单", notes = "【前端加个确认按钮】关闭调拨单")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/closeTransfer")
    public Result closeTransfer(
            Integer headerId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return transferAutoService.backTranSap(headerId,servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 调拨任务列表
     *
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "调拨任务列表", notes = "调拨任务列表")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/searchTransferTask")
    public PagerInfo<List<TransferTaskResponse>> searchTransferTask(
            SearchTransferTaskRequest dto, Pager<?> pager,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return transferAutoService.searchTransferTask(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 调拨任务明细列表
     *
     * @param
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "调拨任务明细列表", notes = "发货单列表")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/searchTransferTaskDtl")
    public List<TransferTaskDtlResponse> searchTransferTaskDtl(
            @ApiParam(value = "调拨单表头ID", required = true)
            @RequestParam(required = true) Integer transferHeaderId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return transferAutoService.searchTransferTaskDtl(transferHeaderId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 点其中一个明细进来查询
     *
     * @param transferDtlId
     * @param transferLineNo
     * @param transferNo
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "调拨任务明细")
    @GetMapping(value = REQUEST_MODEL + "/getTransferTaskDtl/{transferDtlId}/{transferLineNo}/{transferNo}")
    public Result<TransferDtlResp> getTransferTaskDtl(
            @ApiParam(value = "调拨任务明细ID") @PathVariable(value = "transferDtlId") Integer transferDtlId,
            @ApiParam(value = "调拨单行号") @PathVariable(value = "transferLineNo") String transferLineNo,
            @ApiParam(value = "调拨单号") @PathVariable(value = "transferNo") String transferNo,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {

            TransferDtlResp transferTaskDtl = transferAutoService.getTransferTaskDtl(transferDtlId, transferLineNo, transferNo, servletRequest, servletResponse);
            Result<TransferDtlResp> result = new Result<>();
            result.setData(transferTaskDtl);

            return result;
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 扫描SN，返回符合条件的SN内容
     *
     * @param sn
     * @param transferDtlId
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "扫描产品SN")
    @GetMapping(value = REQUEST_MODEL + "/scanSn/{sn}/{transferDtlId}")
    public Result<TransferTaskScanSnResponse> scanSn(
            @ApiParam(value = "产品SN") @PathVariable(value = "sn") String sn,
            @ApiParam(value = "调拨任务明细ID") @PathVariable(value = "transferDtlId") Integer transferDtlId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            Result<TransferTaskScanSnResponse> result = transferAutoService.scanSn(sn, transferDtlId, servletRequest, servletResponse);
            return result;
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "查询已提交的SN")
    @GetMapping(value = REQUEST_MODEL + "/getCommitedSn/{transferDtlId}")
    public Result<List<TransferTaskScanSnResponse>> getCommitedSn(
            @ApiParam(value = "调拨任务明细ID") @PathVariable(value = "transferDtlId") Integer transferDtlId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {

            Result<List<TransferTaskScanSnResponse>> result = transferAutoService.getCommitedSncanSn(transferDtlId, servletRequest, servletResponse);
            return result;
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "提交调拨单")
    @PostMapping(value = REQUEST_MODEL + "/commitTransfer")
    public Result<Integer> commitTransfer(
            @RequestBody CommitTransferReq commitTransferReq,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) throws Exception {
        try {
            Result<Integer> result = transferAutoService.commitTransfer(commitTransferReq, servletRequest, servletResponse);
            return result;
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 原材料调拨单管理列表BS
     *
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "原材料调拨单管理列表BS", notes = "原材料调拨单管理列表BS")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/searchTransferRaw")
    public PagerInfo<List<TransferRawResponse>> searchTransferRaw(
            SearchTransferTaskRequest dto, Pager<?> pager,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return transferAutoService.searchTransferRaw(dto, pager);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 原材料调拨单管理细列表BS
     *
     * @param
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "原材料调拨单管理细列表BS", notes = "原材料调拨单管理细列表BS")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @GetMapping(value = REQUEST_MODEL + "/searchTransferRawDtl")
    public List<TransferRawDtlResponse> searchTransferRawDtl(
            @ApiParam(value = "调拨单表头ID", required = true)
            @RequestParam(required = true) Integer transferHeaderId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return transferAutoService.searchTransferRawDtl(transferHeaderId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "单据类型下拉框", notes = "单据类型下拉框")
    @ApiResponses({@ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class)})
    @RequestMapping(value = REQUEST_MODEL + "/getEnums", method = RequestMethod.GET)
    public List<OrtherAsnEnums> getEnums(
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception{
        try {
            return transferAutoService.getTransferRawEnumsList();
        } catch (Exception e) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 导出原材料调拨单
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导出原材料调拨单",notes = "导出原材料调拨单")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/exportTransferRawReportList")
    public void exportTransferRawReportList(
            SearchTransferTaskRequest dto,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) throws Exception {
        try {
            transferAutoService.exportTransferReportList(dto, servletRequest, servletResponse);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
